package com.baidu.tcpChatDemo2;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;

public class ChatClientReaderThread extends Thread {
    private Socket socket;

    public ChatClientReaderThread(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
            InputStream inputStream = socket.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));

            String msg;
            while ((msg = bufferedReader.readLine()) != null){
                System.out.println("客户端收到" + socket.getRemoteSocketAddress() + "消息: " + msg);
            }
        } catch (IOException e) {
            System.out.println("客户端被踢出");
        }
    }
}
